package com.didi.dididi.demo;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @Description: TODO
 * @Data: 2020-05-15 10:54:36
 * @Pacakge: com.didi.dididi.demo
 * @ClassName: Demo
 * @Version: v1.0.0
 * @Author: dilili
 */
public class Demo {
    /**
     * 获取过去第几天的日期
     *
     * @param past
     * @return
     */
    private static Date getPastDate(int past, Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) - past);
        return calendar.getTime();
    }

    private static List<Date> getPastNDays(Date date, int n) {
        List<Date> dates = new ArrayList<>();
        for (int i = 0; i < n; i ++) {
            dates.add(getPastDate(i, date));
        }
        return dates;
    }

//
//    public static void main(String[] args) throws ParseException {
//        SimpleDateFormat formatToDay = new SimpleDateFormat("yyyy-MM-dd");
//        SimpleDateFormat formatToSecond = new SimpleDateFormat("yyy-MM-dd HH:mm:ss");
//        Date date = formatToDay.parse("2020-05-26");
//        List<Date> last30Dates = getPastNDays(date, 5);
//        String str = "select\n" +
////                "\"ROW\" as \"rowkey\",\n" +
//                "\"ip\",\n" +
//                "\"ip_asset_type\",\n" +
//                "\"ip_asset_name\",\n" +
//                "\"ip_country\",\n" +
//                "\"asset_org_id\",\n" +
//                "\"trace_type\",\n" +
//                "\"trace_method\",\n" +
//                "\"trace_label\",\n" +
//                "\"related_info\",\n" +
//                "\"event_status\",\n" +
//                "\"threat_num\",\n" +
//                "\"threat_start_time\",\n" +
//                "\"trace_status\",\n" +
//                "\"threat_end_time\",\n" +
//                "\"trace_start_time\",\n" +
//                "\"trace_end_time\"\n" +
////                   "count(*) " +
//                "from \"TRACE_SOURCE_LIST2\"\n" +
////                "ORDER BY \"trace_method\" desc,\"threat_start_time\"  desc;";
//                "where\n" +
//                "     ((\"threat_start_time\" >=START_TIME and \"threat_start_time\"<=END_TIME)\n" +
//                "     or\n" +
//                "     (\"threat_start_time\"<=START_TIME and \"threat_end_time\">=START_TIME)\n" +
//
////                "     )\n" +
////                "    limit  50;";
//
//                "     )\n" +
//                "ORDER BY \"trace_method\" desc,\"threat_start_time\"  desc\n" +
//                "    limit  10;";
//
////                "     )\n" +
////                "ORDER BY \"threat_start_time\"  desc\n" +
////                "    limit  50;";
////
////                "     )\n" +
////                "ORDER BY \"trace_method\"  desc\n" +
////                "    limit  50;";
//        for (Date day : last30Dates) {
//            String dataStr = formatToDay.format(day);
//            long startTime = formatToSecond.parse(dataStr + " 00:00:00").getTime();
//            long endTime = formatToSecond.parse(dataStr + " 23:59:59").getTime();
////            System.out.println(dataStr + " : ");
//            String tmp  = str;
//            tmp = tmp.replace("START_TIME", String.valueOf(startTime));
//            tmp = tmp.replace("END_TIME", String.valueOf(endTime));
//            System.out.println(tmp);
////            System.out.println();
////            System.out.println();
//        }
//    }
    public static void main(String[] args) throws ParseException {
//       demoOrigin();
//        demoOptimize();
        demoOptimizeCount();
    }


    public static void demoOptimize() throws ParseException {
        SimpleDateFormat formatToDay = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat formatToSecond = new SimpleDateFormat("yyy-MM-dd HH:mm:ss");
        Date date = formatToDay.parse("2020-05-28");
        List<Date> last30Dates = getPastNDays(date, 100);
        String str = "select\n" +
//                "\"ROW\" as \"rowkey\",\n" +
                "\"ip\",\n" +
                "\"ip_asset_type\",\n" +
                "\"ip_asset_name\",\n" +
                "\"ip_country\",\n" +
                "\"asset_org_id\",\n" +
                "\"trace_type\",\n" +
                "\"trace_method\",\n" +
                "\"trace_label\",\n" +
                "\"related_info\",\n" +
                "\"event_status\",\n" +
                "\"threat_num\",\n" +
                "\"threat_start_time\",\n" +
                "\"trace_status\",\n" +
                "\"threat_end_time\"\n" +
                "\"trace_start_time\",\n" +
                "\"trace_end_time\"\n" +
//                   "count(*) " +
                "from \"TRACE_SOURCE_LIST_OPTIMIZEV3\"\n" +
//                "ORDER BY \"trace_method\" desc,\"threat_start_time\"  desc;";
                "where\n" +
                "     ((\"threat_start_time\" >=START_TIME and \"threat_start_time\"<=END_TIME)\n" +
                "     or\n" +
                "     (\"threat_start_time\"<=START_TIME and \"threat_end_time\">=START_TIME)\n" +

//                "     )\n" +
//                "    limit  50;";

                "     )\n" +
//                "ORDER BY \"trace_method\" desc,\"threat_start_time\"  desc\n" +
                "    limit  10;";

//                "     )\n" +
//                "ORDER BY \"threat_start_time\"  desc\n" +
//                "    limit  50;";
//
//                "     )\n" +
//                "ORDER BY \"trace_method\"  desc\n" +
//                "    limit  50;";
        for (Date day : last30Dates) {
            String dataStr = formatToDay.format(day);
            long startTime = formatToSecond.parse(dataStr + " 00:00:00").getTime();
            long endTime = formatToSecond.parse(dataStr + " 23:59:59").getTime();
//            System.out.println(dataStr + " : ");
            String tmp  = str;
            tmp = tmp.replace("START_TIME", String.valueOf(startTime));
            tmp = tmp.replace("END_TIME", String.valueOf(endTime));
            System.out.println(tmp);
//            System.out.println();
//            System.out.println();
        }
    }
    public static void demoOptimizeCount() throws ParseException {
        SimpleDateFormat formatToDay = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat formatToSecond = new SimpleDateFormat("yyy-MM-dd HH:mm:ss");
        Date date = formatToDay.parse("2020-06-01");
        List<Date> last30Dates = getPastNDays(date, 1);
        String str = "select\n" +
                "count(*) as count\n" +
//                "\"ROW\" as \"rowkey\",\n" +
//                "\"ip\",\n" +
//                "\"ip_asset_type\",\n" +
//                "\"ip_asset_name\",\n" +
//                "\"ip_country\",\n" +
//                "\"asset_org_id\",\n" +
//                "\"trace_type\",\n" +
//                "\"trace_method\",\n" +
//                "\"trace_label\",\n" +
//                "\"related_info\",\n" +
//                "\"event_status\",\n" +
//                "\"threat_num\",\n" +
//                "\"threat_start_time\",\n" +
//                "\"trace_status\",\n" +
//                "\"threat_end_time\"\n" +
//                "\"trace_start_time\",\n" +
//                "\"trace_end_time\"\n" +
//                   "count(*) " +
                "from \"TRACE_SOURCE_LIST_OPTIMIZEV3\"\n" +
//                "ORDER BY \"trace_method\" desc,\"threat_start_time\"  desc;";
                "where\n" +
                "     ((\"threat_start_time\" >=START_TIME and \"threat_start_time\"<=END_TIME)\n" +
                "     or\n" +
                "     (\"threat_start_time\"<=START_TIME and \"threat_end_time\">=START_TIME)\n" +

                "     );\n";
//                "    limit  50;";
//
//                "     )\n" +
//                "ORDER BY \"trace_method\" desc,\"threat_start_time\"  desc\n" +
//                "    limit  10;";
//
//                "     )\n" +
//                "ORDER BY \"threat_start_time\"  desc\n" +
//                "    limit  50;";
//
//                "     )\n" +
//                "ORDER BY \"trace_method\"  desc\n" +
//                "    limit  50;";
        for (Date day : last30Dates) {
            String dataStr = formatToDay.format(day);
            long startTime = formatToSecond.parse(dataStr + " 00:00:00").getTime();
            long endTime = formatToSecond.parse(dataStr + " 23:59:59").getTime();
//            System.out.println(dataStr + " : ");
            String tmp  = str;
            tmp = tmp.replace("START_TIME", String.valueOf(startTime));
            tmp = tmp.replace("END_TIME", String.valueOf(endTime));
            System.out.println(tmp);
//            System.out.println();
//            System.out.println();
        }
    }

    public static void demoOptimizeCount2() throws ParseException {
        SimpleDateFormat formatToDay = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat formatToSecond = new SimpleDateFormat("yyy-MM-dd HH:mm:ss");
        Date date = formatToDay.parse("2020-05-26");
        List<Date> last30Dates = getPastNDays(date, 10);
        String str = "select\n" +
                "count(*) as count\n" +
//                "\"ROW\" as \"rowkey\",\n" +
//                "\"ip\",\n" +
//                "\"ip_asset_type\",\n" +
//                "\"ip_asset_name\",\n" +
//                "\"ip_country\",\n" +
//                "\"asset_org_id\",\n" +
//                "\"trace_type\",\n" +
//                "\"trace_method\",\n" +
//                "\"trace_label\",\n" +
//                "\"related_info\",\n" +
//                "\"event_status\",\n" +
//                "\"threat_num\",\n" +
//                "\"threat_start_time\",\n" +
//                "\"trace_status\",\n" +
//                "\"threat_end_time\"\n" +
//                "\"trace_start_time\",\n" +
//                "\"trace_end_time\"\n" +
//                   "count(*) " +
                "from \"TRACE_SOURCE_LIST_OPTIMIZE\"\n" +
//                "ORDER BY \"trace_method\" desc,\"threat_start_time\"  desc;";
                "where\n" +
                "     ((\"threat_start_time\" >=START_TIME and \"threat_start_time\"<=END_TIME)\n" +
                "     or\n" +
                "     (\"threat_start_time\"<=START_TIME and \"threat_end_time\">=START_TIME)\n" +

                "     );\n";
//                "    limit  50;";
//
//                "     )\n" +
//                "ORDER BY \"trace_method\" desc,\"threat_start_time\"  desc\n" +
//                "    limit  10;";
//
//                "     )\n" +
//                "ORDER BY \"threat_start_time\"  desc\n" +
//                "    limit  50;";
//
//                "     )\n" +
//                "ORDER BY \"trace_method\"  desc\n" +
//                "    limit  50;";
        for (Date day : last30Dates) {
            String dataStr = formatToDay.format(day);
            long startTime = formatToSecond.parse(dataStr + " 00:00:00").getTime();
            long endTime = formatToSecond.parse(dataStr + " 23:59:59").getTime();
//            System.out.println(dataStr + " : ");
            String tmp  = str;
            tmp = tmp.replace("START_TIME", String.valueOf(startTime));
            tmp = tmp.replace("END_TIME", String.valueOf(endTime));
            System.out.println(tmp);
//            System.out.println();
//            System.out.println();
        }
    }


    public static void demoOrigin() throws ParseException {
        SimpleDateFormat formatToDay = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat formatToSecond = new SimpleDateFormat("yyy-MM-dd HH:mm:ss");
        Date date = formatToDay.parse("2020-05-27");
        List<Date> last30Dates = getPastNDays(date, 10);
        String str = "select\n" +
//                "\"ROW\" as \"rowkey\",\n" +
                "\"ip\",\n" +
//                "\"ip_asset_type\",\n" +
//                "\"ip_asset_name\",\n" +
//                "\"ip_country\",\n" +
                "\"asset_org_id\",\n" +
//                "\"trace_type\",\n" +
                "\"trace_method\",\n" +
//                "\"trace_label\",\n" +
//                "\"related_info\",\n" +
//                "\"event_status\",\n" +
//                "\"threat_num\",\n" +
                "\"threat_start_time\",\n" +
//                "\"trace_status\",\n" +
                "\"threat_end_time\"\n" +
//                "\"trace_start_time\",\n" +
//                "\"trace_end_time\"\n" +
//                   "count(*) " +
                "from \"TRACE_SOURCE_LIST_ORIGIN\"\n" +
//                "ORDER BY \"trace_method\" desc,\"threat_start_time\"  desc;";
                "where\n" +
                "     ((\"threat_start_time\" >='START_TIME' and \"threat_start_time\"<='END_TIME')\n" +
                "     or\n" +
                "     (\"threat_start_time\"<='START_TIME' and \"threat_end_time\">='START_TIME')\n" +

//                "     )\n" +
//                "    limit  50;";

                "     )\n" +
                "ORDER BY \"trace_method\" desc,\"threat_start_time\"  desc\n" +
                "    limit  10;";

//                "     )\n" +
//                "ORDER BY \"threat_start_time\"  desc\n" +
//                "    limit  50;";
//
//                "     )\n" +
//                "ORDER BY \"trace_method\"  desc\n" +
//                "    limit  50;";
        for (Date day : last30Dates) {
            String dataStr = formatToDay.format(day);
            String startTime = dataStr + " 00:00:00";
            String endTime = dataStr + " 23:59:59";
//            System.out.println(dataStr + " : ");
            String tmp  = str;
            tmp = tmp.replace("START_TIME", startTime);
            tmp = tmp.replace("END_TIME", endTime);
            System.out.println(tmp);
//            System.out.println();
//            System.out.println();
        }
    }
}

//1,上下级级联数据同步项目：溯源事件数据同步接口编写。
//2，威胁模块7个接口新加目标ip和源ip的省份的匹配和过滤。
//3，溯源追踪查询接口添加目标ip的过滤代码编写。
//4，【攻击事件】攻击事件页面，检索条件中，排除事件类型检索失败代码bug 修复。
//5，公共日志列表接口新加适配大屏方向过滤的条件。
//6，溯源查询接口，查询性能测试和速度优化。

